//
//  WBNewfeatureViewController.m
//  微博项目
//
//  Created by ma c on 16/8/15.
//  Copyright (c) 2016年 beijing. All rights reserved.
//

#import "WBNewfeatureViewController.h"
#import "UIView+WBExtension.h"
#define kCount 4

@interface WBNewfeatureViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView * scrollView;
@property (strong, nonatomic) UIPageControl * page;

@end

@implementation WBNewfeatureViewController

- (UIPageControl *)page{
    if (!_page) {
        _page = [[UIPageControl alloc]init];
        _page.width = 100;
        _page.height = 50;
        _page.centerX = self.scrollView.width * 0.5;
        _page.centerY = self.scrollView.height - 50;
        _page.numberOfPages = kCount;
    }
    return _page;
}


- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        _scrollView.contentSize = CGSizeMake(kCount * _scrollView.width, 0);
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    self.scrollView.delegate = self;
    
}
- (void)setUpUI{
    [self setScrollView];
    [self setPageControl];
}

- (void)setScrollView{
    [self.view addSubview:self.scrollView];
    for (int i = 0; i < kCount; i++) {
        UIImageView * imageView = [[UIImageView alloc]init];
        imageView.size = self.scrollView.size;
        imageView.x = i * imageView.width;
        imageView.y = 0;
        NSString * imageName = [NSString stringWithFormat:@"new_feature_%d",i + 1];
        imageView.image = [UIImage imageNamed:imageName];
        [self.scrollView addSubview:imageView];
        if (i == kCount - 1) {
            [self setShareImageView:imageView];
            [self setStartImageView:imageView];
            
        }
    }
}

- (void)setStartImageView:(UIImageView *)imageView{
    UIButton * startBtn = [[UIButton alloc]init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = imageView.width * 0.5;
    startBtn.centerY = imageView.height * 0.75;
    [startBtn setTitle:@"开启微博" forState:UIControlStateNormal];
    [imageView addSubview:startBtn];
    
    
}
//分享按钮
- (void)setShareImageView:(UIImageView *)imageView{
    
    imageView.userInteractionEnabled = YES;
    
    UIButton * shareBtn = [[UIButton alloc]init];
    shareBtn.width = 200;
    shareBtn.height = 30;
    shareBtn.centerX = imageView.width * 0.5;
    shareBtn.centerY = imageView.height * 0.65;
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage: [UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
    shareBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [imageView addSubview:shareBtn];
    
}

- (void)shareClick:(UIButton *)shareBtn{
    shareBtn.selected = !shareBtn.isSelected;
}


- (void)setPageControl{
    [self.view addSubview:self.page];
    self.page.currentPageIndicatorTintColor = [UIColor redColor];
    self.page.pageIndicatorTintColor = [UIColor yellowColor];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    double page = self.scrollView.contentOffset.x / self.scrollView.width;
    self.page.currentPage = (int)(page + 0.5);
}
@end
